package com.hugoo.core.slt;

import com.hugoo.core.Conf;
import com.hugoo.core.scheduling.RptTaskCollector;
import com.hugoo.core.util.TL;
import com.hugoo.imp.slt.ImpQueue;
import com.hugoo.imp.slt.ImpThread;
import it.sauronsoftware.cron4j.Scheduler;
import it.sauronsoftware.cron4j.TaskCollector;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Web application lifecycle listener.
 *
 * @author Lovelock.Chan
 */
public class ServletListener implements ServletContextListener, HttpSessionListener {

    static final String SCHEDULER = "SCHEDULER_SVL";
    private final static Log log = LogFactory.getLog(ServletListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext sctx = sce.getServletContext();
        String px = sctx.getInitParameter(Conf.RPT_CONFIG);
        if (TL.isEmpty(px)) {
            log.error("系统web.xml中未找到参数【RPT_CONFIG】的配置项，请按照说明配置报表文件路径。");
        } else {
            if (Conf.reload(px)) {
                Enumeration<String> rx = sctx.getInitParameterNames();
                String ne, vl;
                int i = 0;
                while (rx.hasMoreElements()) {
                    ne = rx.nextElement();
                    vl = sctx.getInitParameter(ne);
                    if (TL.isNotEmpty(vl) && vl.toLowerCase().matches("true|y|yes|support")) {
                        if ("scheduler".equalsIgnoreCase(ne) && (i & 1) == 1) {
                            i = i + 1;
                            log.info("==初始化调度工作...");
                            init_scheduler(sce);
                        } else if ("import".equalsIgnoreCase(ne) && (i & 2) == 2) {
                            i = i + 2;
                            log.info("==初始化数据导入线程...");
                            new ImpThread().start();
                        }
                    }
                }
            }
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        // 1. Retrieves the scheduler from the context.
        Scheduler scheduler = (Scheduler) context.getAttribute(SCHEDULER);
        // 2. Removes the scheduler from the context.
        context.removeAttribute(SCHEDULER);
        // 3. Stops the scheduler.
        log.info("Stops the scheduler.");
        scheduler.stop();
        //4、stops the 数据导入线程
        log.info("stops the 数据导入线程");
        ImpQueue.ref().putCode("H--STOP");
        ImpThread.stopImp();
    }

    @Override
    public void sessionCreated(HttpSessionEvent se) {
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
    }

    private void init_scheduler(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        // 1. Creates the scheduler.
        Scheduler scheduler = new Scheduler();
        // 2. Registers a custom task collector.
        TaskCollector collector = new RptTaskCollector();
        scheduler.addTaskCollector(collector);
        // 3. Starts the scheduler.
        scheduler.start();
        // 4. Registers the scheduler.
        context.setAttribute(SCHEDULER, scheduler);
    }
}
